#include <MD5.au3>
#include <IE.au3>
#include <TFEX.au3>
#include <GolfAdlib.au3>
#include <Date.au3>


;Make all position reference become relative
AutoItSetOption("MouseCoordMode", 0)

$c2wUser = "pisutek"
$c2wPass = "inkognito"
$c2wPin = ""

StartStreamingPro($c2wUser, $c2wPass)

$i = 1

While 1
   $productType = IniRead("order_onetime.ini", "ORDER_" & $i, "ProductType", "-")
   If $productType = "-" Then ExitLoop
   
   $contractName = IniRead("order_onetime.ini", "ORDER_" & $i, "ContractName", "-")
   $openOrClose = IniRead("order_onetime.ini", "ORDER_" & $i, "OpenOrClose", "-")
   $txnType = IniRead("order_onetime.ini", "ORDER_" & $i, "TxnType", "-")
   $bidOrOfferOrMP = IniRead("order_onetime.ini", "ORDER_" & $i, "BidOrOfferOrMP", "-")
   $operand = IniRead("order_onetime.ini", "ORDER_" & $i, "Operand", "-")
   $price = Number(IniRead("order_onetime.ini", "ORDER_" & $i, "Price", "0"))
   $status = IniRead("order_onetime.ini", "ORDER_" & $i, "Status", "-")
   
   ConsoleWrite("==================================================" & @CRLF)
   ConsoleWrite("Info: $contractName read   :" & $contractName & @CRLF)
   ConsoleWrite("Info: $openOrClose read    :" & $openOrClose & @CRLF)
   ConsoleWrite("Info: $txnType read        :" & $txnType & @CRLF)
   ConsoleWrite("Info: $bidOrOfferOrMP read :" & $bidOrOfferOrMP & @CRLF)
   ConsoleWrite("Info: $operand read        :" & $operand & @CRLF)
   ConsoleWrite("Info: $price read          :" & $price & @CRLF)
   ConsoleWrite("Info: $status read         :" & $status & @CRLF)
   
   $buyOrSell = ""
   
   If $txnType = "Long" Then
	  $buyOrSell = "Buy"
   ElseIf $txnType = "Short" Then
	  $buyOrSell = "Sell"
   Else
	  ConsoleWrite("Error: unclear whether long or short:" & $txnType & @CRLF)
	  ExitLoop
   EndIf
   
   If $contractName <> "-" And $status <> "PROCESSED" Then
	  
	  WinActivate("Settrade.com : ")
	  
	  Local $FixOrMP = "MP"
	  Local $stopProcess = "N"
	  
	  If $bidOrOfferOrMP = "MP" Then
		 
	  Else
		 
		 $FixOrMP = "Fix"
		 
		 ;Need to check price first
		 $priceOutput = GetTFEXPrice($contractName)
		 
		 WinActivate("Settrade.com : ")
		 
		 ConsoleWrite("Debug:" & $contractName & " price:" & $priceOutput & @CRLF)
		 
		 If $priceOutput <> "none" Then
			
			Local $infoArr = StringSplit($priceOutput, "|")
			Local $date = $infoArr[1]
			Local $time = $infoArr[2]
			Local $bid = Number($infoArr[3])
			Local $high = Number($infoArr[4])
			Local $last = Number($infoArr[5])
			Local $low = Number($infoArr[6])
			Local $offer = Number($infoArr[7])
			
			ConsoleWrite("++++++ PRICE +++++" & @CRLF)
			ConsoleWrite("Debug: $date  = " & $date & @CRLF)
			ConsoleWrite("Debug: $time  = " & $time & @CRLF)
			ConsoleWrite("Debug: $bid   = " & $bid & @CRLF)
			ConsoleWrite("Debug: $high  = " & $high & @CRLF)
			ConsoleWrite("Debug: $last  = " & $last & @CRLF)
			ConsoleWrite("Debug: $low   = " & $low & @CRLF)
			ConsoleWrite("Debug: $offer = " & $offer & @CRLF)
			
			If $price = 0 Then
			   $stopProcess = "Y"
			   ConsoleWrite("Error:" & $contractName & " does not have proper price configured" & @CRLF)
			EndIf
			
			If $bidOrOfferOrMP = "Bid" and $stopProcess = "N" Then

			   If $operand = "gt" And $bid > $price Then
				  $FixOrMP = "MP"
			   EndIf
			   
			   If $operand = "lt" And $bid < $price Then
				  $FixOrMP = "MP"
			   EndIf
			   
			   If $operand = "ge" And $bid >= $price Then
				  $FixOrMP = "MP"
			   EndIf
			   
			   If $operand = "le" And $bid <= $price Then
				  $FixOrMP = "MP"
			   EndIf
			   
			   If $operand = "eq" And $bid = $price Then
				  $FixOrMP = "MP"
			   EndIf

			ElseIf $bidOrOfferOrMP = "Offer" and $stopProcess = "N" Then

			   If $operand = "gt" And $offer > $price Then
				  $FixOrMP = "MP"
			   EndIf
			   
			   If $operand = "lt" And $offer < $price Then
				  $FixOrMP = "MP"
			   EndIf
			   
			   If $operand = "ge" And $offer >= $price Then
				  $FixOrMP = "MP"
			   EndIf
			   
			   If $operand = "le" And $offer <= $price Then
				  $FixOrMP = "MP"
			   EndIf
			   
			   If $operand = "eq" And $offer = $price Then
				  $FixOrMP = "MP"
			   EndIf

			EndIf
			
		 Else
			ConsoleWrite("Error:" & $contractName & " does not seem to exist" & @CRLF)
			$stopProcess = "Y"
		 EndIf
		 
	  EndIf
	  
	  If $FixOrMP = "MP" and $stopProcess = "N" Then
		 autoTFEX_MP_ByContractName($buyOrSell, $openOrClose, $contractName, 1, $c2wPin)
		 IniWrite("order_onetime.ini", "ORDER_" & $i, "Status", "PROCESSED")
		 IniWrite("order_onetime.ini", "ORDER_" & $i, "ProcessDate", _Date_Time_SystemTimeToDateTimeStr(_Date_Time_GetSystemTime()))
		 
		 ;====================
		 ;**TODO: Remove this temp code before use as this is to remove any error message dialog during test
		 ;====================
		 Sleep(1000)
		 Send("{ENTER}")
		 
	  EndIf
	  
	  
   EndIf
   
   $i = $i + 1
WEnd

Exit













WinActivate("Settrade.com : ")
WinWaitActive("Settrade.com : ")

;Make sure that the size is right
WinMove("", "", 50, 0, 1458, 1079)

;Select Derivatives
MouseClick("", 263, 739)
MouseClick("", 143, 804)

;Select Buy side
MouseClick("", 31, 787)

;Capture screen shot
;Move the mouse away before switch to settrade to prevent the highlight
MouseMove(1443,926)
Sleep(500)
Send("!{PRINTSCREEN}")

;start paint
Run("C:\WINDOWS\system32\mspaint.exe")
WinWaitActive("untitled - Paint")

MakePictureSmall()

WinWaitActive("untitled - Paint")
Sleep(125)

;paste the captured screen
Send("^v")
Sleep(125)
send("{ESC}")

;Delete the name of the contract which is possibly there
MouseClickDrag("", 297, 822, 476, 846)
Send("{DEL}")

;select the Buy/Sell panel
MouseClickDrag("", 70, 810, 1511, 892)
Sleep(125)
Send("^c")
Send("{ESC}")
Sleep(125)
MakePictureSmall()

;paste the buy/sell panel image
Send("^v")

;Remove the old latest image File
FileDelete($newSettradeImgFile)

;now save the File
Send("^s")
WinWaitActive("Save As")
Send($newSettradeImgFile)
Send("{ENTER}")

;Wait for the control to come back to paint window
WinWaitActive($newSettradeImgFileName & " - Paint")

;Close the paint
Send("!{F4}")

$originalHash = _FileHash_MD5($settradeImgFile)
MsgBox(0, "DEBUG", "originalHash=" & $originalHash)
$currentHash = _FileHash_MD5($newSettradeImgFile)
MsgBox(0, "DEBUG", "currentHash=" & $currentHash)

If $originalHash <> $currentHash Then
   MsgBox(48, "Attention", "There is potentially differences with the Settrade Window layout")
Else
   ;MsgBox(0, "DONE", "FINISHED")
EndIf





;====================================================
;      Custom Function area
;====================================================
Func MakePictureSmall()
   ;Change the size of the blank picture to the smallest possible
   Send("^e")
   WinWaitActive("Attributes")

   ;Change width and height to 4 x 4 Pixels
   Sleep(100)
   Send("4")
   Send("{TAB}")
   Sleep(100)
   Send("4")
   Sleep(100)
   Send("{ENTER}")
EndFunc

